package leetcode;

public class DeleteDuplicates {
    static class ListNode {
        int val;
        ListNode next;
        ListNode() {}
        ListNode(int val) { this.val = val; }
        ListNode(int val, ListNode next) {
            this.val = val; this.next = next;
        }
    }
    public static void main(String[] args) {

        ListNode node2 = getNode1();
        ListNode head = deleteDuplicates(node2);
        while (head != null) {
            System.out.print(head.val + " ");
            head = head.next;
        }while (head != null) {
            System.out.print(head.val + " ");
            head = head.next;
        }
    }
    public static ListNode deleteDuplicates(ListNode head) {

            if (head == null) {
                return head;
            }
            // 排序的链表，重复的元素一定是连续的
            ListNode cur = head;
            while (cur != null) {
                if(cur.next != null && cur.val == cur.next.val) {
                    cur.next = cur.next.next;
                }else{
                    cur = cur.next;
                }
            }
        return head;
    }
    private static ListNode getNode1() {
        // 创建第一个节点
        ListNode head = new ListNode(1);
        System.out.println(head);
        // 用于遍历和连接节点的指针
        // 此处使用的是引用地址
        ListNode current = head;
        // 创建并连接后续节点
        current.next = new ListNode(1);
        current = current.next;

        current.next = new ListNode(2);
        current = current.next;

        current.next = new ListNode(3);
        current = current.next;

        current.next = new ListNode(3);

        return head;
    }
    private static ListNode getNode2() {
        // 创建第一个节点
        ListNode head = new ListNode(3);
        System.out.println(head);
        // 用于遍历和连接节点的指针
        // 此处使用的是引用地址
        ListNode current = head;
        // 创建并连接后续节点
        current.next = new ListNode(3);
        current = current.next;

        current.next = new ListNode(7);
        current = current.next;

        current.next = new ListNode(10);
        current = current.next;

        current.next = new ListNode(12);
        current = current.next;

        current.next = new ListNode(14);
        return head;
    }
}
